package Task24;

import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JList;

import org.apache.log4j.Logger;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.security.acl.LastOwnerException;
import java.util.ArrayList;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

public class ShowWindow {
	private static final Logger Log = Logger.getLogger(ShowWindow.class);
	private JFrame frmSuperCommander;
	private JList list;
	private String command;
	private String wichListRefresh = "left";

	public String getCommand() {
		return command;
	}

	public void setCommand(String myCommand) {
		command = myCommand;
	}

	public String getWichListRefresh() {
		return wichListRefresh;
	}

	public void setWichListRefresh(String value) {
		wichListRefresh = value;
	}

	public void show() {
		frmSuperCommander.setVisible(true);
	}

	public void hide() {
		frmSuperCommander.setVisible(false);
	}

	public void setText(ArrayList<String> text) {
		DefaultListModel myModel = new DefaultListModel();

		for (int i = 0; i < text.size(); i++) {
			myModel.addElement(text.get(i));
		}
		list.setModel(myModel);
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ShowWindow window = new ShowWindow();
					window.frmSuperCommander.setVisible(true);
				} catch (Exception e) {
					Log.error(e.getMessage());
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ShowWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmSuperCommander = new JFrame();
		frmSuperCommander.setTitle("Super Show Window");
		frmSuperCommander.setBounds(100, 100, 568, 344);

		JScrollPane scrollPane = new JScrollPane();

		JButton btnNewButton = new JButton("Close");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				hide();
			}
		});
		GroupLayout groupLayout = new GroupLayout(
				frmSuperCommander.getContentPane());
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								Alignment.TRAILING,
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																scrollPane,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																422,
																Short.MAX_VALUE)
														.addComponent(
																btnNewButton,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																90,
																Short.MAX_VALUE))
										.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE,
								216, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(btnNewButton).addContainerGap()));

		list = new JList();
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (arg0.getClickCount() == 2) {
					Object selected = list.getSelectedValue();
					;
					String file = (String) selected;
					if (command.startsWith("find "))
						openFileForFind(file);
				}
			}
		});
		scrollPane.setViewportView(list);
		frmSuperCommander.getContentPane().setLayout(groupLayout);
	}

	private void openFileForFind(String file) {
		String[] arrFile = file.split("-");
		file = arrFile[2];

		try {
			Runtime.getRuntime().exec("notepad.exe " + file);
		} catch (IOException e) {
			Log.error(e.getMessage());
		}
	}

	
	
}
